import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    //用队列实现栈；
    private Queue<Integer>  queue1;
    private Queue<Integer> queue2;
    public MyStack(){
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }
    //模拟实现向栈中放入元素；
    public void push(int val){
        if(!queue1.isEmpty()){
            queue1.offer(val);
        } else if (!queue2.isEmpty()) {
            queue2.offer(val);
        }else{
            queue1.offer(val);
        }
    }
    //模拟实现删除栈中元素；
    public int pop(){
        if(empty()){
            return -1;
        }
        if(!queue1.isEmpty()){
            int size=queue1.size();
            while(size-1>0){
                queue2.offer(queue1.poll());
                size--;
            }
            return queue1.poll();
        }else{
            int size= queue2.size();
            while(size-1>0){
                queue1.offer(queue2.poll());
                size--;
            }
            return queue2.poll();
        }

    }
    public boolean empty(){
        return queue1.isEmpty()&&queue2.isEmpty();
    }
    //获取栈中的元素；
    public int peek(){
        if(empty()){
            return -1;
        }
        if(!queue1.isEmpty()){
            int size= queue1.size();
            int tmp=-1;
            while(size>0){
                tmp=queue1.poll();
                queue2.offer(tmp);
                size--;
            }
            return tmp;
        }else{
            int size= queue2.size();
            int tmp=-1;
            while(size!=0){
                tmp=queue2.poll();
                queue1.offer(tmp);
                size--;
            }
            return tmp;
        }
    }
}
